package com.css.fxfzxczh.modules.remoteSenseData.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.remoteSenseData.entity.RemoteSense;
import com.css.fxfzxczh.modules.remoteSenseData.repository.RemoteSenseRepository;
import com.css.fxfzxczh.modules.remoteSenseData.vo.RemoteSenseVO;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.web.PlatformSessionContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2024/3/11 16:32
 * @Description:
 */
@Repository
public class RemoteSenseRepositoryImpl implements RemoteSenseRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String xsemId) {
        StringBuilder sql = new StringBuilder(" SELECT a.*,b.structure_type,b.structure_proportion,b.destroy,b.serious_damage, " +
                "b.medium_damage,b.slight_damage,b.basically_intact,b.floor_space  FROM xczh_remote_sense a left join " +
                "xczh_remote_sense_supplement b on a.id=b.xrs_id");
        StringBuilder whereSql = new StringBuilder(" where a.del_flag='"+YNEnum.N+"' ");
        if(PlatformObjectUtils.isNotEmpty(xsemId)){
            whereSql.append(" and a.xsem_id = '"+xsemId+"'");
        }
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(RemoteSenseVO.class)).size();
        List<RemoteSenseVO> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(RemoteSenseVO.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void batchSave(List<RemoteSense> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());

        String sql = "INSERT INTO xczh_remote_sense " +
                "( id,code,boundary,reliability,xsem_id,del_flag,create_time,create_user)" +
                "values (:id,:code,:boundary,:reliability,:xsemId,:delFlag,:createTime,:createUser)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void delete(List<String> ids) {
        String sql = "update xczh_remote_sense set del_flag = '" + YNEnum.Y.toString() + "'where id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<RemoteSenseVO> getDataListByXsemId(String xsemId) {
        StringBuilder sql = new StringBuilder(" SELECT a.*,b.structure_type,b.structure_proportion,b.destroy,b.serious_damage, " +
                "b.medium_damage,b.slight_damage,b.basically_intact,b.floor_space  FROM xczh_remote_sense a left join " +
                "xczh_remote_sense_supplement b on a.id=b.xrs_id");
        StringBuilder whereSql = new StringBuilder(" where a.del_flag='"+YNEnum.N+"' ");
        if(PlatformObjectUtils.isNotEmpty(xsemId)){
            whereSql.append(" and a.xsem_id = '"+xsemId+"'");
        }



        List<RemoteSenseVO> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(RemoteSenseVO.class));

        return list;
    }
}
